/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license text file included in
 * the distribution.
 */


// Per-pixel lighting - fragment shader side.

uniform sampler2D qt_Texture0;
uniform sampler2D qt_Texture1;

varying mediump vec3 qNormal;
varying mediump vec4 qAmbient;
varying mediump vec4 qDiffuse;
varying mediump vec4 vertex;
varying mediump vec3 toLight;
varying highp vec4 qt_TexCoord0;
varying mediump vec4 qSpecular;
varying mediump float qShininess;


void main(void)
{
    mediump vec4 color = texture2D(qt_Texture0, qt_TexCoord0.st);
    mediump vec4 reflectionColor = texture2D(qt_Texture1, vertex.xy * 0.2 +
                                             qNormal.xy * 0.5);

    mediump float lmul = dot(normalize(qNormal), normalize(toLight));

    mediump float clmul = clamp(abs(lmul) * lmul, 0.0, 1.0);
    clmul = clmul * clmul / (0.9 + dot(toLight, toLight) * 0.0005);

    lmul = clamp(-lmul, 0.0, 1.0);

    gl_FragColor = vec4(color.xyz * (qDiffuse.xyz * vec3(clmul, clmul, clmul) +
                                     qAmbient.xyz * vec3(lmul,lmul,lmul)) +
                        reflectionColor.xyz * qShininess +
                        (qSpecular.xyz * color.w),
                        clamp(5.0 + vertex.z * 0.05, 0.0, 1.0));
}
